package com.aric.springbootmusic.mapper;

import com.aric.springbootmusic.entity.pojo.Permission;
import com.aric.springbootmusic.entity.pojo.Role;
import com.aric.springbootmusic.entity.pojo.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Select("""
              SELECT
              		ps_name
              from
              	  `user`,role,rp,permission
              WHERE
              	   `user`.role_id=role.role_id and role.role_id=rp.role_id and rp.ps_id=permission.ps_id and `user`.id=#{id} 
            """)
    List<String> selectPermissionsByUserId(String id);


    //优化一下，这里写的太复杂了，开始
    @Results(value = {
            @Result(id = true,column = "ps_id",property = "psId"),
            @Result(column = "ps_name",property = "psName"),
            @Result(column = "ps_cname",property = "psCname"),
            @Result(column = "ps_path",property = "psPath")
    })
    @Select("""
                select 
                    ps_id,ps_name,ps_cname,ps_path 
                from 
                    permission 
                where 
                    permission.ps_pid=#{psPid}
    """)
    List<Permission> selectPermissionByPsPid(Integer psPid);

    @Results(value = {
            @Result(id = true,column = "ps_id",property = "psId"),
            @Result(column = "ps_name",property = "psName"),
            @Result(column = "ps_cname",property = "psCname"),
            @Result(column = "ps_id",property = "children",many = @Many(select = "com.aric.springbootmusic.mapper.UserMapper.selectPermissionByPsPid")),
    })
    @Select("""
        select p.ps_id,ps_name,ps_cname,ps_pid from permission as p,rp where rp.ps_id=p.ps_id and p.ps_level=0 and rp.role_id=#{rid}
    """)
    List<Permission> selectPermissionByRoleId(Integer rid);

    @Results(value = {
            @Result(id = true,column = "role_id",property = "roleId"),
            @Result(column = "role_name",property = "roleName"),
            @Result(column = "role_desc",property = "roleDesc"),
            @Result(column = "role_id",property = "permissionList",many = @Many(select = "com.aric.springbootmusic.mapper.UserMapper.selectPermissionByRoleId"))
    })
    @Select("""
            select role_id,role_name,role_desc from role where role_id=#{rid}
            """)
    Role selectRoleById(int rid);

    @Results(value = {
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "nickname",property = "nickname"),
            @Result(column = "role_id",property = "role",one = @One(select = "com.aric.springbootmusic.mapper.UserMapper.selectRoleById"))
    })
    @Select("""
            SELECT
            	id,username,nickname,role_id
            from
            	`user`
            where
                id=#{uid}	
            """)
    User selectMenuByUserId(String uid);
    //优化一下，这里写的太复杂了，结尾

    @Results(id="findUserResultSet",value = {
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "nickname",property = "nickname"),
            @Result(column = "email",property = "email"),
            @Result(column = "phone_number",property = "phoneNumber"),
            @Result(column = "status",property = "status"),
            @Result(column = "role_id",property = "role.roleId"),
            @Result(column = "role_name",property = "role.roleName")
    })

    @Select("<script> "+
                "select "+
                    "id,username,nickname,email,phone_number,status,user.role_id,role_name "+
                "from "+
                     "user,role "+
                "<where> "+
                    "user.role_id=role.role_id  and user.is_delete=0"+
                    "<if test='keyword != null and keyword != \"\"'> " +
                        "and username like '%' #{keyword} '%' or nickname like '%' #{keyword} '%' or email like '%' #{keyword} " +
                    "</if> "+
                "</where> "+
            "</script>")
    /*@Select("""
            select
                id,username,nickname,email,phone_number,status,`user`.role_id,role_name
            from
                `user`,role
            <where>
                `user`.role_id=role.role_id
                <if test='keyword != null and keyword != ""'>
                    and username like '%' #{keyword} '%' or nickname like '%' #{keyword} '%' or email like '%' #{keyword}
                </if>
            </where>
            """)*/
    Page<User> selectPageVo(@Param("page") Page<User> page, @Param("keyword") String keyword);

    //查询单个用户的信息(包含角色)
    @ResultMap(value="findUserResultSet")
    @Select("<script> "+
                "select "+
                    "id,username,nickname,email,phone_number,status,user.role_id,role_name "+
                "from "+
                    "user,role "+
                "<where> "+
                    "user.role_id=role.role_id and user.id=#{id}"+
                "</where> "+
            "</script>")
    User selectUserById(String id);

    @Select("""
            select 
                ps_name 
            from 
                `user`,role,rp,permission 
            where 
                `user`.id=#{id} and `user`.role_id=role.role_id and role.role_id=rp.role_id and rp.ps_id=permission.ps_id and permission.ps_level=2     
    """)
    List<String> findAllUserAuthority(String id);


    @Select("""
        select role_name from user,role where user.role_id=role.role_id and user.id=#{id}
    """)
    String findUserRoleById(String id);
}
